@echo off

rem ########################################################################
rem # Test: Script to invoke Periscope using Sick Beard's Parameters
rem # It should properly download subtitles for each finished / succesfully
rem # processed download. Well, in fact it does.
rem #
rem # Alvaro "Effenberg" Leal <AlvaroLeal.Backup@GMail.com>, 2011
rem # Get the latest code at http://code.google.com/p/call-periscope/
rem #
rem # Converted to cmd and updated by n2k
rem #########################################################################

rem #SET THIS OR IT WILL NOT WORK
Set PERISCOPE_BIN="C:\Python26\Scripts\periscope"
Set PERISCOPE_LANGS=-l nl -l en
Set PERISCOPE_DUPE=1
Set CALL_PERISCOPE_LOG="C:\Program Files (x86)\SickBeard\CallPeriscope.log"

rem #########################################################################

rem #Other args and variables needed for the script, leave it alone.
for %%I in (%1) do Set PROCESSED_EPISODE_PATH=%%~dpI
for %%I in (%1) do Set PROCESSED_EPISODE_NAME=%%~nI
for %%I in (%2) do Set ORIGINAL_EPISODE_NAME=%%~nI
for %%I in (%2) do Set ORIGINAL_EPISODE_EXT=%%~xI

rem #Log file handling, if its bigger then 1MiB it'll delete it and create a new file.
for %%R in (%CALL_PERISCOPE_LOG%) do if %%~zR GTR 1048576 del %CALL_PERISCOPE_LOG%
echo - >> %CALL_PERISCOPE_LOG%
echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% >> %CALL_PERISCOPE_LOG%
echo - >> %CALL_PERISCOPE_LOG%

rem #Debug - too see if its being invoked at all
for /f "delims=" %%A in ('python %PERISCOPE_BIN% --version ^| findstr /v "void"') do set OutputVariable=%%A
echo Periscope version: %OutputVariable% >> %CALL_PERISCOPE_LOG%

rem #Check what arguments we're getting from SickBeard
echo CallPeriscope.cmd Received arg 1: %1 >> %CALL_PERISCOPE_LOG%
echo CallPeriscope.cmd Received arg 2: %2 >> %CALL_PERISCOPE_LOG%

rem #Check if we processed it right and got what we needed
echo Processed Episode Path is %PROCESSED_EPISODE_PATH% >> %CALL_PERISCOPE_LOG%
echo Processed Episode File Name is %PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT% >> %CALL_PERISCOPE_LOG%
echo Original Episode File Name is %ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT% >> %CALL_PERISCOPE_LOG%

rem #Change working directory to processed episode path
echo Changing Directory to provided episode path. From "%CD%\" to "%PROCESSED_EPISODE_PATH%" >> %CALL_PERISCOPE_LOG%
cd %PROCESSED_EPISODE_PATH%

rem #Renaming the episode so periscope can actually find some results (hack to get around -q bug)
ren "%PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%"

rem #Call Periscope with proper command line args. Now its not up to us..
echo Invoking Periscope: python %PERISCOPE_BIN% %PERISCOPE_LANGS% %ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT% >> %CALL_PERISCOPE_LOG%
python %PERISCOPE_BIN% %PERISCOPE_LANGS% %ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%

rem #Check if Periscope found a subtitle, if not goto dupe script option
if NOT %ErrorLevel%==0 GOTO duplicate
echo Found a subtitle! >> %CALL_PERISCOPE_LOG%
ren "%ORIGINAL_EPISODE_NAME%.srt" "%PROCESSED_EPISODE_NAME%.srt"

:exit
rem #Rename the episode back
ren "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" "%PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%"
echo Exiting... >> %CALL_PERISCOPE_LOG%
GOTO eof

:duplicate
rem #Check if we need to duplicate the script to the processed episode path
if NOT %PERISCOPE_DUPE%==1 (
  echo Didn't find any subtitles. >> %CALL_PERISCOPE_LOG%
  GOTO exit
)
echo Didn't find any subtitles. Creating duplicate file at "%CD%\%PROCESSED_EPISODE_NAME%.cmd" >> %CALL_PERISCOPE_LOG%
echo @echo off > "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo ren "%PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo python %PERISCOPE_BIN% %PERISCOPE_LANGS% "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo if NOT %%ErrorLevel%%==0 ( >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   ren "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" "%PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   echo Didn't find any subtitles, exiting... >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo ) else ( >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   ren "%ORIGINAL_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" "%PROCESSED_EPISODE_NAME%%ORIGINAL_EPISODE_EXT%" >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   ren "%ORIGINAL_EPISODE_NAME%.srt" "%PROCESSED_EPISODE_NAME%.srt" >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   echo Found a subtitle! Deleting myself... >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo   del %%0 >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
echo ) >> "%CD%\%PROCESSED_EPISODE_NAME%.cmd"
GOTO exit

:eof
rem #Clear all used variables
Set PERISCOPE_BIN=
Set PERISCOPE_LANGS=
Set CALL_PERISCOPE_LOG=
Set PROCESSED_EPISODE_PATH=
Set PROCESSED_EPISODE_NAME=
Set ORIGINAL_EPISODE_NAME=
Set ORIGINAL_EPISODE_EXT=
Set OutputVariable=